home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / gs24src.zip / GSUTIL.C < prev    next >
C/C++ Source or Header  |  1992-03-20  |  5KB  |  154 lines

  1. /* Copyright (C) 1992 Aladdin Enterprises.  All rights reserved.
  2.    Distributed by Free Software Foundation, Inc.
  3.  
  4. This file is part of Ghostscript.
  5.  
  6. Ghostscript is distributed in the hope that it will be useful, but
  7. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  8. to anyone for the consequences of using it or for whether it serves any
  9. particular purpose or works at all, unless he says so in writing.  Refer
  10. to the Ghostscript General Public License for full details.
  11.  
  12. Everyone is granted permission to copy, modify and redistribute
  13. Ghostscript, but only under the conditions described in the Ghostscript
  14. General Public License.  A copy of this license is supposed to have been
  15. given to you along with Ghostscript so you can know your rights and
  16. responsibilities.  It should be in a file named COPYING.  Among other
  17. things, the copyright notice and this notice must be preserved on all
  18. copies.  */
  19.  
  20. /* gsutil.c */
  21. /* Utilities for Ghostscript library */
  22. #include "std.h"
  23. #include "string_.h"
  24. #include "memory_.h"
  25. #include "gsprops.h"
  26. #include "gsutil.h"            /* for prototype checking */
  27.  
  28. /* ------ String utilities ------ */
  29.  
  30. /* Compare two strings, returning -1 if the first is less, */
  31. /* 0 if they are equal, and 1 if first is greater. */
  32. /* We can't use memcmp, because we always use unsigned characters. */
  33. int
  34. bytes_compare(const byte *s1, uint len1, const byte *s2, uint len2)
  35. {    register uint len = len1;
  36.     if ( len2 < len ) len = len2;
  37.        {    register const byte *p1 = s1;
  38.         register const byte *p2 = s2;
  39.         while ( len-- )
  40.             if ( *p1++ != *p2++ )
  41.                 return (p1[-1] < p2[-1] ? -1 : 1);
  42.        }
  43.     /* Now check for differing lengths */
  44.     return (len1 == len2 ? 0 : len1 < len2 ? -1 : 1);
  45. }
  46.  
  47. /* Test whether a string matches a pattern with wildcards. */
  48. /* '*' = any substring, '?' = any character, '\' quotes next character. */
  49. int
  50. string_match(const byte *str, uint len, const byte *pstr, uint plen,
  51.   int ignore_case)
  52. {    const byte *pback = 0;
  53.     const byte *p = pstr, *pend = pstr + plen;
  54.     const byte *sp = str, *spend = str + len;
  55.     uint matched = 0;
  56.     while ( p < pend )
  57.        {    byte ch = *p;
  58.         switch ( ch )
  59.            {
  60.         case '*':
  61.             pback = ++p, matched = 0;
  62.             continue;
  63.         case '?':
  64.             if ( sp == spend ) return 0;    /* str too short */
  65.             p++, sp++, matched++;
  66.             continue;
  67.         case '\\':
  68.             if ( ++p == pend ) return 1;    /* bad pattern */
  69.             ch = *p;
  70.            }
  71.         if ( sp == spend ) return 0;    /* str too short */
  72.         if ( *sp == ch || ignore_case && (*sp ^ ch) == 0x20 &&
  73.              (ch &= ~0x20) >= 0x41 && ch <= 0x5a
  74.            )
  75.             p++, sp++, matched++;
  76.         else if ( pback == 0 )
  77.             return 0;    /* no * to back up to */
  78.         else
  79.            {    sp += 1 - matched;
  80.             p = pback;
  81.             matched = 0;
  82.            }
  83.        }
  84.     return 1;
  85. }
  86.  
  87. /* Compute a hash for a string */
  88. uint
  89. string_hash(const byte *ptr, uint len)
  90. {    register const byte *p = ptr;
  91.     register uint hash = 0;
  92.     register uint n = len;
  93.     while ( n-- ) hash = hash * 19 + *p++;
  94.     return hash;
  95. }
  96.  
  97. /* ------ Property list utilities ------ */
  98.  
  99. /* Extract known properties from a list.  See gsutil.h for more details. */
  100. int
  101. props_extract(gs_prop_item *plist, int count,
  102.   const gs_prop_item *template, int tcount,
  103.   gs_prop_item **pknown, int finished)
  104. {    const gs_prop_item *pti;
  105.     gs_prop_item **ppi;
  106.     gs_prop_item *pli;
  107.     int i, j;
  108.     for ( pti = template, ppi = pknown, j = 0; j < tcount; pti++, ppi++, j++ )
  109.        {    char *tstr = pti->pname;
  110.         int tlen = pti->name_size;
  111.         if ( tstr == 0 ) continue;    /* no name */
  112.         if ( tlen < 0 ) tlen = strlen(tstr);
  113.         *ppi = 0;
  114.         for ( pli = plist, i = 0; i < count; pli++, i++ )
  115.           if ( pli->status == pv_set )
  116.            {    char *lstr = pli->pname;
  117.             int llen = pli->name_size;
  118.             if ( lstr == 0 ) continue;
  119.             if ( llen < 0 ) llen = strlen(lstr);
  120.             if ( llen == tlen && !memcmp(tstr, lstr, llen) )
  121.                {    /* Names match, check types. */
  122.                 if ( pli->type != pti->type )
  123.                  { /* Check for int [array] -> float [array] */
  124.                    if ( pli->type == prt_int &&
  125.                     pti->type == prt_float
  126.                       )
  127.                      pli->type = prt_float,
  128.                      pli->value.f = pli->value.i;
  129.                    else if ( pli->type == prt_int_array &&
  130.                          pti->type == prt_float_array
  131.                        )
  132.                     { int i;
  133.                       gs_prop_item *vp = pli->value.a.p.v;
  134.                       pli->type = prt_float_array;
  135.                       for ( i = pli->value.a.size; --i >= 0; vp++ )
  136.                     vp->type = prt_float,
  137.                     vp->value.f = vp->value.i;
  138.                     }
  139.                    else
  140.                     { pli->status = pv_typecheck;
  141.                       break;
  142.                     }
  143.                  }
  144.                 *ppi = pli, pli->status = pv_OK;
  145.                 break;
  146.                }
  147.            }
  148.        }
  149.     if ( finished )
  150.       for ( pli = plist, i = 0; i < count; pli++, i++ )
  151.         if ( pli->status != pv_OK ) pli->status = pv_unknown;
  152.     return 0;
  153. }
  154.